package com.dietdroid;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ConsumedFoodsActivity extends ListActivity {
	
	private ArrayList<String> consumed_foods;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.consumed_foods);
        int year = this.getIntent().getIntExtra("YEAR", -1);
        int month = this.getIntent().getIntExtra("MONTH", -1);
        int day = this.getIntent().getIntExtra("DAY", -1);
        consumed_foods = DietDataOperations.getConsumedFoods(month, day, year, this.getApplicationContext());
        if(consumed_foods == null) {
        	this.setListAdapter(null);
        }else {
            ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, consumed_foods);
            this.setListAdapter(adapter);
        }
        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            	String item = consumed_foods.get(position);
            	
                FoodItemDataBase fdb = FoodItemDataBase.getInstance();
                FoodItem fooditem = fdb.getFoodItem(item);
                
                Intent myIntent = new Intent(ConsumedFoodsActivity.this.getApplicationContext(), FoodInfoActivity.class);

                Bundle b = new Bundle();
                b.putSerializable("food", fooditem);
                myIntent.putExtras(b);
                
                startActivityForResult(myIntent, 0);
            }
          });
	}
	
	
}
